/* Author : Amalia Nuryamin	 091511037
 * 			Putri Andita R   091511059
 * Proyek 4*/

package jtk.project4.fleet.screen.addEditPartInventory;

import java.awt.FlowLayout;
import java.util.List;


import jtk.project4.fleet.action.CancelAction;
import jtk.project4.fleet.action.CancelActionPartInventory;
import jtk.project4.fleet.action.SubmitAction;
import jtk.project4.fleet.action.SubmitActionPartInventory;
import jtk.project4.fleet.event.InventoryEvent;
import jtk.project4.fleet.event.VendorEvent;
import jtk.project4.fleet.field.PartIdentificationField;
import jtk.project4.fleet.field.PartInventoryField;
import jtk.project4.fleet.field.PartInventoryTableField;
import jtk.project4.fleet.field.PhotoField;
import jtk.project4.fleet.field.PhotoTombolField;
import jtk.project4.fleet.field.QuantityField;


import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormField;
import nl.coderight.jazz.form.FormHeader;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.control.TabControl;
import nl.coderight.jazz.form.control.TabItem;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;

public class AddEditPartInventoryView extends FormView{
	private FormHeader formHeader;
	private TabControl partinventoryTab;
	private TabItem identificationTab;
	private TabItem inventoryTab;
	private TabItem photoTab;
	
	private Action submitAction;
	private Action cancelAction;
	
	private PushButton saveButton;
	private PushButton cancelButton;

	
	public void handleEvent(InventoryEvent evt) {
		switch (evt.getType()) {
		case CANCEL:
			close();
			break;
			
		case SUBMIT:
			// check for validation errors
			List<ValidationError> errors = validate();
			if (!errors.isEmpty()) {
				// there are validation errors so do not submit...
				errors.get(0).getField().focus();
				break;
			} else {
				// submit form and close view
				submit();
				close(); // fall-through!
			}			
		default:
			// propagate event to controller
			propagateEvent(evt);
			break;
		}
	}
	@Override
	protected void init() {
		// TODO Auto-generated method stub
		createActions();
		//createTabFields();		
		createFields();
		createLayout();
	}
	
	private void createActions(){
		submitAction = new SubmitActionPartInventory();
		cancelAction = new CancelActionPartInventory();
	}
	
	private void createLayout() {
		setHeader(createHeader());
		addField(createForm());
	}
	
	private FormHeader createHeader() {
		formHeader = new FormHeader();
		formHeader.setTitle("addnewpart.header.form");
		return formHeader;
	}
	
	private void createFields() {
		saveButton = new PushButton(submitAction);
		cancelButton = new PushButton(cancelAction);
		partinventoryTab = new TabControl();
		identificationTab = new TabItem("Identification");
		inventoryTab = new TabItem("Inventory");
		photoTab = new TabItem("Photo");
	}
	
//	private void createTabFields()
//	{
//		partinventoryTab = new TabControl();
//		identificationTab = new TabItem("Identification");
//		inventoryTab = new TabItem("Inventory");
//		photoTab = new TabItem("Photo");
//	}
	
	private FormField createTabs()
	{
		TabControl form = partinventoryTab;
		form.addTab(identificationTab, createIdentificationTab());
		form.addTab(inventoryTab, createInventoryTab());
		form.addTab(photoTab, createPhotoTab());
		return form;
	}
	
	
	
	private GroupControl createIdentificationTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())	
		.addField(new PartIdentificationField("Parts")) //identification
		.addRow();		
		return form;
	}
	
	private GroupControl createPhotoTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new PhotoField("Parts")) //Photo	
		.addRow()
		.addField(new PhotoTombolField("Parts"))	;		
		return form;
	}
	
	private GroupControl createInventoryTab() {
		GroupControl form = new GroupControl();
		form.setLayout(new FormLayout())
		.addField(new PartInventoryField("Parts")) //inventory	
		.addRow()
		.addField(new PartInventoryTableField("Parts"))
		.addRow()
		.addField(new QuantityField("Parts"));
			
		return form;
	}
	
		
		private GroupControl createButtons() {
		GroupControl buttons = new GroupControl();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
		buttons.addField(saveButton);
		buttons.addField(cancelButton);
		return buttons;
	}
	
	
private FormField createForm(){
	GroupControl form = new GroupControl();
	form.addField(createTabs());
	form.addField(createButtons(), SOUTH);
	return form;
}



private GroupControl createGeneralForm() {
	GroupControl form = new GroupControl();
	form.addField(createGeneralForm(), NORTH);
	form.addField(createButtons(), SOUTH);
	return form;
}
}
